Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.owl2java;
16:
17: import cz.cvut.kbss.jopa.model.SequencesVocabulary;
18: import cz.cvut.kbss.jopa.util.MappingFileParser;
19: import java.io.File;
20: import java.net.URI;
21: import java.util.ArrayList;
22: import java.util.Arrays;
23: import java.util.Collection;
24: import java.util.HashMap;
25: import java.util.List;
26: import java.util.Map;
27: import org.semanticweb.owlapi.apibinding.OWLManager;
28: import org.semanticweb.owlapi.model.IRI;
29: import org.semanticweb.owlapi.model.OWLAnnotation;
30: import org.semanticweb.owlapi.model.OWLAnnotationValueVisitor;
31: import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
32: import org.semanticweb.owlapi.model.OWLAxiom;
33: import org.semanticweb.owlapi.model.OWLDataFactory;
34: import org.semanticweb.owlapi.model.OWLEntity;
35: import org.semanticweb.owlapi.model.OWLLiteral;
36: import org.semanticweb.owlapi.model.OWLOntology;
37: import org.semanticweb.owlapi.model.OWLOntologyCreationException;
38: import org.semanticweb.owlapi.model.OWLOntologyManager;
39: import org.semanticweb.owlapi.util.OWLOntologyMerger;
40: import org.slf4j.Logger;
41: import org.slf4j.LoggerFactory;
42:
43: public class OWL2JavaTransformer {
44:
45: public static final String P_IS_INTEGRITY_CONSTRAINT_FOR = "http://krizik.felk.cvut.cz/ontologies/2009/ic.owl#isIntegrityConstraintFor";
46: private static final Logger LOG = LoggerFactory.getLogger(OWL2JavaTransformer.class);
47: private static final ContextDefinition DEFAULT_CONTEXT = new ContextDefinition("<DEFAULT>");
48:
49: private static final List<IRI> skipped = Arrays
50: .asList(IRI.create(SequencesVocabulary.c_Collection), IRI.create(SequencesVocabulary.c_List),
51: IRI.create(SequencesVocabulary.c_OWLSimpleList),
52: IRI.create(SequencesVocabulary.c_OWLReferencedList));
53: private final ValidContextAnnotationValueVisitor v = new ValidContextAnnotationValueVisitor();
54: private OWLOntology ontology;
55: private Map<String, ContextDefinition> contexts = new HashMap<>();
56:
57: public Collection<String> listContexts() {
58: return contexts.keySet();
59: }
60:
61: private OWLOntology getWholeOntology(final String owlOntologyName, final String mappingFile) {
62: // reader
63: final OWLOntologyManager m = OWLManager.createOWLOntologyManager();
64:
65: if (mappingFile != null) {
66: LOG.info("Using mapping file '{}'.", mappingFile);
67:
68: final Map<URI, URI> map = MappingFileParser.getMappings(new File(mappingFile));
69: m.addIRIMapper(ontologyIRI -> {
70: final URI value = map.get(ontologyIRI.toURI());
71:
72: if (value == null) {
73: return null;
74: } else {
75: return IRI.create(value);
76: }
77: });
78: LOG.info("Mapping file successfully parsed.");
79: }
80:
81: LOG.info("Loading ontology {} ... ", owlOntologyName);
82: m.setSilentMissingImportsHandling(true);
83:
84: try {
85: m.loadOntology(org.semanticweb.owlapi.model.IRI.create(owlOntologyName));
86: return new OWLOntologyMerger(m)
87: .createMergedOntology(m, org.semanticweb.owlapi.model.IRI.create(owlOntologyName + "-generated"));
88: } catch (OWLOntologyCreationException e) {
89: LOG.error(e.getMessage(), e);
90: throw new IllegalArgumentException("Unable to load ontology " + owlOntologyName, e);
91: }
92: }
93:
94: private void addAxiomToContext(final ContextDefinition ctx, final OWLAxiom axiom) {
95: final OWLDataFactory f = ontology.getOWLOntologyManager().getOWLDataFactory();
96:
97: for (final OWLEntity e : axiom.getSignature()) {
98: if (e.isOWLClass() && !skipped.contains(e.getIRI())) {
99: ctx.classes.add(e.asOWLClass());
100: }
101: if (e.isOWLObjectProperty() && !skipped.contains(e.getIRI())) {
102: ctx.objectProperties.add(e.asOWLObjectProperty());
103: }
104: if (e.isOWLDataProperty() && !skipped.contains(e.getIRI())) {
105: ctx.dataProperties.add(e.asOWLDataProperty());
106: }
107: if (e.isOWLAnnotationProperty() && !skipped.contains(e.getIRI())) {
108: ctx.annotationProperties.add(e.asOWLAnnotationProperty());
109: }
110: if (e.isOWLNamedIndividual() && !skipped.contains(e.getIRI())) {
111: ctx.individuals.add(e.asOWLNamedIndividual());
112: }
113: }
114: ctx.axioms.add(axiom);
115: }
116:
117: public void setOntology(final String owlOntologyName,
118: final String mappingFile, boolean includeImports) {
119: ontology = getWholeOntology(owlOntologyName, mappingFile);
120:
121: // this.imports = ontology.getOWLOntologyManager().getOntologies();
122:
123: LOG.info("Parsing integrity constraints");
124:
125: for (final OWLAxiom a : ontology.getAxioms()) {
126: addAxiomToContext(DEFAULT_CONTEXT, a);
127: for (final String icContextName : getContexts(a)) {
128: ContextDefinition ctx = getContextDefinition(icContextName);
129: addAxiomToContext(ctx, a);
130: }
131: }
132:
133: DEFAULT_CONTEXT.parse();
134: for (final ContextDefinition ctx : contexts.values()) {
135: ctx.parse();
136: }
137:
138: LOG.info("Integrity constraints successfully parsed.");
139: }
140:
141: private ContextDefinition getContextDefinition(String icContextName) {
142: ContextDefinition ctx = contexts.get(icContextName);
143: if (ctx == null) {
144: ctx = new ContextDefinition(icContextName);
145: contexts.put(icContextName, ctx);
146: }
147: return ctx;
148: }
149:
150: private List<String> getContexts(final OWLAxiom a) {
151: final List<String> contexts = new ArrayList<>();
152: for (final OWLAnnotation p : a.getAnnotations()) {
153: LOG.info("Processing annotation : " + p);
154: if (!p.getProperty().getIRI().toString().equals(P_IS_INTEGRITY_CONSTRAINT_FOR)) {
155: continue;
156: }
157: p.getValue().accept(v);
158: final String icContextName = v.getName();
159: LOG.info("CONTEXT:" + icContextName);
160: if (icContextName == null) {
161: continue;
162: }
163: LOG.debug("Found IC {} for context {}", a, icContextName);
164: contexts.add(icContextName);
165: }
166: return contexts;
167: }
168:
169: private void verifyContextExistence(String context) {
170: if (!contexts.containsKey(context)) {
171: throw new IllegalArgumentException(
172: "Context " + context + " not found. Existing contexts: " + listContexts());
173: }
174: }
175:
176: public void transform(String context, String pkg, String targetDir, boolean withOWLAPI) {
177: LOG.info("Transforming context ...");
178: if (context == null) {
179: LOG.info(" - for all axioms");
180: } else {
181: LOG.info(" - for context '{}'.", context);
182: verifyContextExistence(context);
183: }
184:
185: ContextDefinition def = context == null ? DEFAULT_CONTEXT : contexts.get(context);
186: new JavaTransformer().generateModel(ontology, def, pkg, targetDir, withOWLAPI);
187: LOG.info("Transformation SUCCESSFUL.");
188: }
189:
190: /**
191: * Generates only vocabulary of the loaded ontology.
192: *
193: * @param context Integrity constraints context, if null is supplied, the whole ontology is interpreted as integrity constraints.
194: * @param targetDir Directory into which the vocabulary file will be generated
195: * @param pkg Package
196: * @param withOWLAPI Whether OWLAPI-based IRIs of the generated vocabulary items should be created as well
197: */
198: public void generateVocabulary(String context, String pkg, String targetDir, boolean withOWLAPI) {
199: LOG.info("Generating vocabulary ...");
200: if (context == null) {
201: LOG.info(" - for all axioms");
202: } else {
203: LOG.info(" - for context '{}'.", context);
204: verifyContextExistence(context);
205: }
206: ContextDefinition def = (context == null) ? DEFAULT_CONTEXT : contexts.get(context);
207: new JavaTransformer().
208: generateVocabulary(ontology, def, pkg, targetDir, withOWLAPI);
209: }
210:
211: private class ValidContextAnnotationValueVisitor implements OWLAnnotationValueVisitor {
212: private String name = null;
213:
214: String getName() {
215: return name;
216: }
217:
218: public void visit(IRI iri) {
219: }
220:
221: public void visit(OWLAnonymousIndividual individual) {
222: }
223:
224: public void visit(OWLLiteral literal) {
225: name = literal.getLiteral();
226: }
227: }
228: }